<template>
  <el-container class="Home-container">
    <el-header>
      <div>
        <img
          src="https://www.baidu.com/img/flexible/logo/pc/result.png"
          height="50"
          alt=""
        />
        <span> 后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="true"
          router
          :default-active="curPath"
        >
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconsobj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item
              :index="'/' + subitem.path"
              v-for="subitem in item.children"
              :key="subitem.id"
              @click="setcurPath('/' + subitem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subitem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data () {
    return {
      menulist: [],
      iconsobj: {
        '125': 'el-icon-platform-eleme', 
        '103': 'el-icon-platform-eleme',
        '101': 'el-icon-eleme',
        '102': 'el-icon-s-tools',
        '145': 'el-icon-s-tools'
      },
      isCollapse: false,
      curPath: ''
    }
  },
  created () {
    this.getMenuList()
    this.curPath = window.sessionStorage.getItem('curPath')
  },
  methods: {
    setcurPath (v) {
     window.sessionStorage.setItem('curPath',v)
      this.curPath = v
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      this.menulist = res.data
      console.log(res)
    },
    logout () {
      window.sessionStorage.clear();
      this.$router.push('/login');
    }
  }
};
</script>
<style lang="less" scoped>
.Home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    > span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
